/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* UsedOrderPopup.java
*
* Created on December 28, 2007, 3:59 PM
 */
package gui.genomemap.popup;

import genomemap.data.OrderElement;

import gui.genomemap.outputModel.OutputModel;
import gui.genomemap.view.View;

import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author  stewari1
 */
public class UsedOrderPopup extends javax.swing.JPanel {

    /** Field description */
    public static final String TABLE = "table";

    /** Field description */
    private View view = null;


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JScrollPane jScrollPane1;

    /** Field description */
    private javax.swing.JTable table;


    // End of variables declaration//GEN-END:variables

    /** Creates new form UsedOrderPopup */
    private UsedOrderPopup() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table        = new javax.swing.JTable();

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Select an Order"));
        table.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

            { null, null, null, null }, { null, null, null, null }, { null, null, null, null },
            { null, null, null, null }

        }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        table.setCellSelectionEnabled(true);
        jScrollPane1.setViewportView(table);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375,
                javax.swing.GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275,
                javax.swing.GroupLayout.PREFERRED_SIZE));

    }    // </editor-fold>//GEN-END:initComponents

    public static JPopupMenu createPopup(View view) {

        JPopupMenu popup                    = new JPopupMenu();
        final UsedOrderPopup usedOrderPanel = new UsedOrderPopup();

        usedOrderPanel.setView(view);

        final OutputModel[] outputModels =
            usedOrderPanel.getView().getManager().getOutputModelCollection().toArray(
                new OutputModel[0]);
        final String[] colNames = { "ChId", "OrderCount", "Order" };


        // create the table model
        TableModel tableModel = new AbstractTableModel() {

            public void addTableModelListener(TableModelListener l) {}
            public void removeTableModelListener(TableModelListener l) {}
            public Class<?> getColumnClass(int columnIndex) {
                return String.class;
            }
            public int getColumnCount() {
                return 3;
            }
            public int getRowCount() {
                return outputModels.length;
            }
            public Object getValueAt(int rowIndex, int columnIndex) {

                switch (columnIndex) {

                    case 0 :
                        return outputModels[rowIndex].getInputModel().getLinkageGroup();

                    case 1 :
                        return outputModels[rowIndex].getInputModel().getOrderCount();

                    case 2 :
                        return OrderElement.createOrderString(
                            outputModels[rowIndex].getInputModel().getOrderSet());

                    default :
                        return "";
                }
            }
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
            public String getColumnName(int columnIndex) {
                return colNames[columnIndex];
            }
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {}
        };

        usedOrderPanel.getTable().setModel(tableModel);
        popup.add(usedOrderPanel);
        popup.putClientProperty(UsedOrderPopup.TABLE, usedOrderPanel.getTable());
        popup.setBorderPainted(false);

        return popup;
    }

    JTable getTable() {
        return table;
    }

    View getView() {
        return view;
    }

    void setView(View view) {
        this.view = view;
    }
}
